//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    selectPerson: true,
    firstPerson: '请选择项目',
    selectArea: false,
    motto: 'Hello World',
    userInfo: {},
    serviceItem: [],
    show_nav: "h_nav",
    itemList:[],
    itemid:'',
    hid:'',
    pitemList:[]

  },

  //点击选择类型
  clickPerson: function (e) {
    var that = this;
    
    that.setData({
      itemid: that.data.itemList[e.detail.value].item_id,
      firstPerson: that.data.itemList[e.detail.value].item_name
    });
  },
  //点击切换
  mySelect: function (e) {
    var that = this;
    var _ism = wx.getStorageSync("_ism");

    this.setData({
      firstPerson: e.target.dataset.me,
      firstPersonId: e.target.dataset.itemid,
      selectPerson: true,
      selectArea: false,
    })
  },

  show_nav: function (e) {

      var that = this;

      that.setData({

        show_nav: 'Haircut_nav'

      });

  },
  onLoad: function (e) {
    console.log(e);
   
      var that = this;
     var pages = getCurrentPages();
                  var prevPage = pages[pages.length - 2]; 
                  prevPage.setData({
                    show_nav: 'h_nav'
                  });

    //调用应用实例的方法获取全局数据
    that.setData({
      userInfo: wx.getStorageSync("userInfo"),
      hid:e.hid
    })
    wx.showLoading({
      title: '加载中',
    }),
      wx.request({
      url: app.globalData.api_url + "index.php?ac=hapi&ts=personal_item_list",
      data: { hid: e.hid },
        header: { 'content-type': 'application/json' },
        success: function (res) {
          wx.hideLoading();
          that.setData({
            pitemList: res.data.list,
            itemList:res.data.itemList
          })
          console.log(res.data);
        }

      })
      , setTimeout(function () {
        wx.hideLoading()
      }, 30000)
  },
  personalItemAdd: function (e) {
    console.log(e.detail.value);
    var sdata = e.detail.value;
    if (sdata.price == '' || parseFloat(sdata.price) < parseFloat(sdata.fencheng) || parseInt(sdata.itemid) == 0 ){
      wx.showToast({
        title: '请填写完整或者填写正确再提交!',
        icon:'loading',
        duration:1000
      });return ;
    } 
    var that = this;
    wx.request({
      url: app.globalData.api_url + "index.php?ac=hapi&ts=personal_item_add",
      data: {
        hid: that.data.hid,
        item_id: that.data.itemid,
        price:e.detail.value.price,
        fencheng:e.detail.value.fencheng
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        if (res.data.result == 0) {
          wx.showToast({
            title: '添加失败!',
            icon: 'loading',
            duration: 2000
          }); 
        } else {
          wx.showToast({
            title: '添加成功!',
            icon: 'success',
            duration: 2000
          })
        };
        that.setData({
          pitemList: res.data.list
        });
        setTimeout(function () {
          wx.hideToast()
        }, 5000);
        console.log(res.data);
      }

    })

  },
  delItem: function (e) {
    console.log(e.currentTarget.dataset);
    var that = this;
    wx.request({
      url: app.globalData.api_url + "index.php?ac=hapi&ts=personal_item_del",
      data: {
        hid: e.currentTarget.dataset.hairdresser_id,
        id: e.currentTarget.dataset.id
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        if (res.data.result == 0) {
          wx.showToast({
            title: '操作失败!',
            icon: 'loading',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '操作成功!',
            icon: 'success',
            duration: 2000
          });
          that.setData({
            pitemList: res.data.list
          });
        };
        
        setTimeout(function () {
          wx.hideToast()
        }, 2000);
      }

    })
  },

})
